*************************************************
*			02_repliication_figures				*
*												*
*	This do file runs code produce the 			* 
*	Appendix replication figures in Ricks ® 	*
*	Kay "Time-Limited Subsidies"				*
*												*
*												*
*												*
*************************************************


* prepare 
clear all
set more off, perm
cap log close

* Packages (uncomment if needed)
* ssc install gtools

***
* Set Macros Here
***

* set directory
global wind 	"/Users/michaeldavidricks/Documents/timelimits_replication"
cap mkdir 		"${wind}/results"

* Set colors for graphs
global ptb5 "54 75 154"
global ptb4 "74 123 183"
global ptb3 "110 166 205"
global ptb1 "194 228 239"
global ptr3 "246 126 75"
global ptr5 "165 0 38"

* Set font for graphs 
*		(only uncomment this line if you have the font installed)
*graph set window fontface "CMU Serif"



***
*  Appendix Figure 1 a: EV sales (Lohawala)
***

/* Import and clear propriatary Automotive News Data Center data
*      Included Models:   GM (Bolt  CT6  Volt) Tesla (Model 3  Model S  Model X)   
* 	   Time Range:    1/2016 - 12/2023


	* Reshape data to make/model/month level
	import excel "${wind}/data_raw/replication/ev_replication1_data.xlsx", clear firstrow case(lower)
	egen group = group(make model year)
	reshape long month , i(group) j(mon)
	replace month = subinstr(month,",","",.)
	destring month, replace

	* Calculate EV Maker Sales by year and quarter 
	gen q = ceil(mon/3)
	bys maker year q: gegen sales = total(month)
	drop month
	replace year = year + q/4 +.125

	* Collapse to Make-Year Level
	keep make year sales q
	duplicates drop

	save "${wind}/data_final/ev_replication.dta", replace
	
*/

* Use collapsed data 
use "${wind}/data_final/ev_replication.dta", clear
keep if make =="GM"

* Rescale Sales to Percent Change Relative to Pre-period
sum sales if inrange(year,2016.75 ,2019.25)
gen pct_sales = sales/`r(mean)'*100 -100


* Depict Percent Changes over time

tw (connected pct_sales year if make == "GM", lcolor("$ptb5") mcolor("$ptb5")) ///
   if inrange(year,2016.75 ,2022.5), ylabel(-100(50)50 , angle(0) format(%3.0f) nogrid) ///
	ytitle("Percentage Change in GM EV Sales" "(Relative to Fully Subsidized Years)" " ") ///
    xlabel(2017(1)2023, nogrid) xscale(r(2016.5 2022.5)) xtitle(" " "Year") ///
	yline(0, lcolor(black)) xline(2020, lcolor(gs14) lw(13) lp(l) ) xline(2019.5, lcolor(gs12) lw(13) lp(l)) ///
	graphregion(color(white)) xsize(8) ///
	text(-90 2019.75 "GM Subsidy" "Phase Out",  box bcolor(white) margin(medsmall) lc(gs8) al(middle)) 

	
graph export "${wind}/results/A1_ev_figure.pdf", replace




***
*  Appendix Figure 1 b: Soda sales (Schmacker and Smed)
***

* These data of the scatter points from Figure 2 in Schmacker and Smed (2023)
*      generously provided by the authors


use "${wind}/data_final/soda_replication.dta", clear

* Aggregate to quarters
gen count = 1
gen round = ceil((_n)/3)
gcollapse (sum) high low  count (mean)  year, by(round)

* Aggregate over low/high self control
gen all = (low+high)/2

* Calculate percent changes relative to taxed period
foreach v in high low all {
  replace `v' = `v'*3/count	
  sum `v' if year<=2013.5 & year>2012
  gen pct_`v' = `v'/`r(mean)'*100 -100
}

	
* Depict Percent Changes over time
	
tw (connected pct_all year , lcolor("$ptb5") mcolor("$ptb5")) ///
   , ylabel(-30(15)45 , angle(0) nogrid) yscale(r(-30.1 30.1)) ytitle("Percent Change in Soda Consumption" "(Relative to 2012-2013)" " ") ///
    xlab(2010(1)2015, nogrid) xscale(r(2009.8 2015.2)) xtitle(" " "Year") ///
	yline(0, lcolor(black)) xline(2012, lcolor(gs12) lp(_)) xline(2013.75, lcolor(gs14) lw(16) lp(l) ) ///
	graphregion(color(white)) xsize(8) ///
	text(-25 2012 "Soda Tax" "Rate Hike", box bcolor(white) margin(medsmall) lc(gs8) al(middle)) text(-25 2013.75  "Soda Tax" "Repeal",  box bcolor(white) margin(medsmall) lc(gs8) al(middle))

	
graph export "${wind}/results/A1_soda_figure.pdf", replace
